package com.lznAdmin.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lznAdmin.base.lznAdminResult;
import com.lznAdmin.system.entity.SysPermission;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @program: lzn-manage-parent
 * @description: 权限管理
 * @author: lzn
 * @create: 2021-09-01 15:33
 **/
@Slf4j
@RestController
@RequestMapping("/sys/permission")
public class SysPermissionController extends BaseController {

    @ApiOperation(value = "资源列表数据")
    @PreAuthorize("hasAuthority('sys:permission:list')")
    @GetMapping("/list")
    public lznAdminResult getPermissionData() {
        Page<SysPermission> page = new Page<>(1, 10000);
        Page<SysPermission> sysPermissionPage = sysPermissionService.page(page, new QueryWrapper<SysPermission>().orderByAsc("order_num"));
        return lznAdminResult.ok(sysPermissionPage);
    }


    @ApiOperation(value = "新增用户")
    @PreAuthorize("hasAuthority('sys:permission:save')")
    @PostMapping("/save")
    public lznAdminResult addPermission(@Validated @RequestBody SysPermission sysPermission) {

        log.info("sysPermission: {}", sysPermission);
        Boolean aBoolean = sysPermissionService.savePermission(sysPermission);
        if (aBoolean) {
            return lznAdminResult.ok();
        }
        return lznAdminResult.no("保存失败！");
    }

    @ApiOperation(value = "编辑用户")
    @PreAuthorize("hasAuthority('sys:permission:edit')")
    @PutMapping("/edit")
    public lznAdminResult editPermission(@Validated @RequestBody SysPermission sysPermission) {

        log.info("sysPermission: {}", sysPermission);
        Boolean aBoolean = sysPermissionService.updatePermission(sysPermission);
        if (aBoolean) {
            return lznAdminResult.ok();
        }
        return lznAdminResult.no("保存失败！");
    }

    @ApiOperation(value = "删除用户")
    @PreAuthorize("hasAuthority('sys:permission:delete')")
    @DeleteMapping("/del")
    public lznAdminResult delPermission(@RequestBody SysPermission sysPermission) {

        boolean b = sysPermissionService.removeById(sysPermission.getId());
        if (b) {
            return lznAdminResult.ok();
        }
        return lznAdminResult.no("保存失败！");
    }
}
